package com.fary.beans.factory;

import com.fary.core.MethodParameter;
import com.fary.util.Assert;
import com.fary.util.ObjectUtils;

import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Field;
import java.lang.reflect.Member;

public class InjectionPoint {

    // 包装函数参数时用于保存所包装的函数参数，内含该参数的注解信息
    protected MethodParameter methodParameter;

    // 包装成员属性时用于保存所包装的成员属性
    protected Field field;

    // 包装成员属性时用于保存所包装的成员属性的注解信息
    private volatile Annotation[] fieldAnnotations;


    /**
     * 构造函数，用于包装一个函数参数
     * Create an injection point descriptor for a method or constructor parameter.
     *
     * @param methodParameter the MethodParameter to wrap
     */
    public InjectionPoint(MethodParameter methodParameter) {
        Assert.notNull(methodParameter, "MethodParameter must not be null");
        this.methodParameter = methodParameter;
    }

    /**
     * 构造函数，用于包装一个成员属性
     * Create an injection point descriptor for a field.
     *
     * @param field the field to wrap
     */
    public InjectionPoint(Field field) {
        Assert.notNull(field, "Field must not be null");
        this.field = field;
    }

    /**
     * 复制构造函数
     * Copy constructor.
     *
     * @param original the original descriptor to create a copy from
     */
    protected InjectionPoint(InjectionPoint original) {
        this.methodParameter = original.methodParameter != null ? new MethodParameter(original.methodParameter) : null;
        this.field = original.field;
        this.fieldAnnotations = original.fieldAnnotations;
    }

    /**
     * 缺省构造函数，出于子类序列化目的
     * Just available for serialization purposes in subclasses.
     */
    protected InjectionPoint() {
    }


    /**
     * 返回所包装的函数参数，仅在当前对象用于包装函数参数时返回非null
     * Return the wrapped MethodParameter, if any.
     * <p>Note: Either MethodParameter or Field is available.
     *
     * @return the MethodParameter, or {@code null} if none
     */
    public MethodParameter getMethodParameter() {
        return this.methodParameter;
    }

    /**
     * 返回所包装的成员属性，仅在当前对象用于包装成员属性时返回非null
     * Return the wrapped Field, if any.
     * <p>Note: Either MethodParameter or Field is available.
     *
     * @return the Field, or {@code null} if none
     */
    public Field getField() {
        return this.field;
    }

    /**
     * 获取所包装的函数参数，不会为null，如果当前对象包装的不是函数参数则抛出异常IllegalStateException
     * Return the wrapped MethodParameter, assuming it is present.
     *
     * @return the MethodParameter (never {@code null})
     * @throws IllegalStateException if no MethodParameter is available
     * @since 5.0
     */
    protected final MethodParameter obtainMethodParameter() {
        Assert.state(this.methodParameter != null, "Neither Field nor MethodParameter");
        return this.methodParameter;
    }

    /**
     * 获取所包装的依赖(方法参数或者成员属性)上的注解信息
     * Obtain the annotations associated with the wrapped field or method/constructor parameter.
     */
    public Annotation[] getAnnotations() {
        if (this.field != null) {
            Annotation[] fieldAnnotations = this.fieldAnnotations;
            if (fieldAnnotations == null) {
                fieldAnnotations = this.field.getAnnotations();
                this.fieldAnnotations = fieldAnnotations;
            }
            return fieldAnnotations;
        } else {
            return obtainMethodParameter().getParameterAnnotations();
        }
    }

    /**
     *
     * 获取所包装的依赖(方法参数或者成员属性)上的指定类型为annotationType的注解信息，如果该类型的注解不存在，则返回null
     * Retrieve a field/parameter annotation of the given type, if any.
     *
     * @param annotationType the annotation type to retrieve
     * @return the annotation instance, or {@code null} if none found
     * @since 4.3.9
     */
    public <A extends Annotation> A getAnnotation(Class<A> annotationType) {
        return (this.field != null ? this.field.getAnnotation(annotationType) :
                obtainMethodParameter().getParameterAnnotation(annotationType));
    }

    /**
     * 获取所包装的依赖(方法参数或者成员属性)的类型
     * Return the type declared by the underlying field or method/constructor parameter,
     * indicating the injection type.
     */
    public Class<?> getDeclaredType() {
        return (this.field != null ? this.field.getType() : obtainMethodParameter().getParameterType());
    }

    /**
     * 1.如果所包装的依赖是成员属性则返回该成员属性，
     * 2.如果所包装的依赖是成员方法参数,则返回对应的成员方法
     * Returns the wrapped member, containing the injection point.
     * @return the Field / Method / Constructor as Member
     */
    public Member getMember() {
        return (this.field != null ? this.field : obtainMethodParameter().getMember());
    }

    /**
     * 1.如果所包装的依赖是成员属性则返回该成员属性
     * 2.如果所包装的依赖是成员方法参数,则返回对应的成员方法
     *  可以认为该方法和 getMember()等价
     * Return the wrapped annotated element.
     * <p>Note: In case of a method/constructor parameter, this exposes
     * the annotations declared on the method or constructor itself
     * (i.e. at the method/constructor level, not at the parameter level).
     * Use {@link #getAnnotations()} to obtain parameter-level annotations in
     * such a scenario, transparently with corresponding field annotations.
     *
     * @return the Field / Method / Constructor as AnnotatedElement
     */
    public AnnotatedElement getAnnotatedElement() {
        return (this.field != null ? this.field : obtainMethodParameter().getAnnotatedElement());
    }


    @Override
    public boolean equals(Object other) {
        if (this == other) {
            return true;
        }
        if (getClass() != other.getClass()) {
            return false;
        }
        InjectionPoint otherPoint = (InjectionPoint) other;
        return (ObjectUtils.nullSafeEquals(this.field, otherPoint.field) &&
                ObjectUtils.nullSafeEquals(this.methodParameter, otherPoint.methodParameter));
    }

    @Override
    public int hashCode() {
        return (this.field != null ? this.field.hashCode() : ObjectUtils.nullSafeHashCode(this.methodParameter));
    }

    @Override
    public String toString() {
        return (this.field != null ? "field '" + this.field.getName() + "'" : String.valueOf(this.methodParameter));
    }

}